package com.mayn.action;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.mayn.common.JfinalInit;

/**
 * 重新提交邮单订阅
 * 
 * @author Administrator
 *
 */
public class ExtraExpressSubscribeAction {

	private static final Logger LOG = LoggerFactory.getLogger(ExtraExpressSubscribeAction.class);

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JfinalInit.InitDb(false, null);

		LOG.info("数据库初始化成功");

		update();
	}

	public static void update() {

		LOG.info("开始重新提交订阅信息");
		List<Record> expressList = null;
		String listSql = "SELECT EIS_NO,EIS_POST_TYPE,EIS_STATE FROM express_info_sync WHERE EIS_DEL = 1 AND EIS_STATE = 0 AND EIS_CREATE_DATETIME BETWEEN date_sub(now(), INTERVAL 2 month) and date_sub(now(), INTERVAL 48 HOUR)";
		LOG.info("sql=" + listSql);
		expressList = Db.find(listSql);
		if (expressList.size() >= 1) {
			LOG.info("共有" + expressList.size() + "个快递单信息需要重新订阅");
			for (int i = 0; i < expressList.size(); i++) {
				String ShipperCode = expressList.get(i).getStr("EIS_POST_TYPE");
				String LogisticCode = expressList.get(i).getStr("EIS_NO");
				int state = KdniaoAction.getExpressOrderState(ShipperCode, LogisticCode);
				if (state == 0 || state == 2) {
					if (KdniaoAction.SubscribeExpressInfo(ShipperCode, LogisticCode)) {
						LOG.info("提交订阅成功:" + ShipperCode + "--" + LogisticCode);
					} else {
						LOG.error("提交订阅失败:" + ShipperCode + "--" + LogisticCode);
					}
				}
			}
		} else {
			LOG.info("没有快递信息重新订阅");
		}

	}

}
